package apiday06.thread;

public class SyncDemo1 {
    private static boolean success1 = false;
    private static boolean success2 = false;
    public static void main(String[] args) {
        int count = 1;
        while(true){
            Bank bank = new Bank();
            Thread t1 = new Thread(){
                @Override
                public void run() {
                    success1 = bank.getMoney(20000);
                }
            };
            Thread t2 = new Thread(){
                @Override
                public void run() {
                    success2 = bank.getMoney(20000);
                }
            };
            t1.start();
            t2.start();

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(success1&&success2){
                System.out.println("全部取款成功");
                System.out.println("共测试："+count+"次");
                break;
            }else {
                bank.saveAccount(20000);
                System.out.println("只有一人取款成功");
                count++;
                success1 = false;
                success2 = false;
                if(count==1000){
                    break;
                }
            }
        }
    }
}


